const WebSocket = require('ws');

// 替换为你的WebSocket服务端点
const ws = new WebSocket('ws://localhost:8081/websocket');

ws.on('open', function open() {
    console.log('Connected to the WebSocket server.');
    // 连接打开后，可以发送消息给服务器
    ws.send('Hello Server!');
});

ws.on('close', function close() {
    console.log('Disconnected from the WebSocket server.');
});

ws.on('error', function error(err) {
    // 处理连接错误
    console.error('WebSocket encountered error:', err);
});

ws.on('message', function message(data, flags) {
    console.log('Received message from server:', data.toString());
});